package com.example.zklock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestLock {
    static int n = 0;

    public static void add() {
        System.out.println(++n);
    }

    public static void main(String[] args) {
        Runnable runnable = () -> {
            ZKDistributeLock lock = null;
            try {
                lock = new ZKLock("192.168.64.200:2081", "test");
                lock.lock();
                add();
                System.out.println(Thread.currentThread().getName() + "is running");
            } catch (Exception e) {

                e.printStackTrace();
            } finally {
                if (lock != null) {
                    lock.unLock();
                }
            }

        };
        ExecutorService executor = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 10; i++) {
            executor.submit(runnable);
        }
        executor.shutdown();
    }
}
